/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of the KiWi Project nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributor(s):
 * sschaffe
 *
 * File Info:
 * User: sschaffe
 * Date: 2011/01/06
 * Time: 15:04
 *
 * Project: KiWi2, kiwi-core module
 */

package kiwi.core.api.user;

import kiwi.core.exception.UserExistsException;
import kiwi.core.model.rdf.KiWiResource;
import kiwi.core.model.user.KiWiUser;

/**
 * At the moment just a dummy for creating the currentUser; should be extended by the KiWi 1.0 UserService functionality
 * <p/>
 * User: sschaffe
 */
public interface UserService {

    /* Current User Management */

    /**
     * Return the currently active user. The method tries to determine the current user using the following means:
     * - the user stored in the session, if existant
     * - the user authenticated using HTTP authentication, if existant
     * - the anonymous user
     *
     * @return
     */
    public KiWiUser getCurrentUser();


    /**
     * Set the current user to the user  passed as argument. The current user should be associated with
     * the current session in a thread local variable that is cleared again when the request finishes
     * (KiWiUserTokenFilter)
     * @param user
     */
    public void setCurrentUser(KiWiUser user);

    /**
     * Clear a current user setting for the current thread. Clears the thread local variable set for the
     * currently running thread.
     */
    public void clearCurrentUser();





    /* User Token Management */

    /**
     * Create a new user token for the user passed as argument. The user token can be used to identify the user
     * throughout a session
     *
     * @param user
     * @return
     */
    public UserToken createUserToken(KiWiUser user);


    /**
     * Get the user token with the given ID, if it exists. If the user token does not exist, returns null.
     *
     * @param tokenId
     * @return
     */
    public UserToken getUserToken(String tokenId);


    /**
     * Make the user token passed as argument invalid so that it cannot be used to identify a user anymore.
     * @param token
     */
    public void invalidateUserToken(UserToken token);




    /**
     * Create a new user with the provided login. The method first
     * checks of a user with this login already exists; if yes, an exception is thrown. The
     * method does not persist the user; this needs to be done by the caller.
     *
     * @param login login of the user to create
     * @return the newly created user.
     */
    public KiWiUser createUser(String login, String password) throws UserExistsException;


    /**
     * Create a new user with the provided login, first name and last name. The method first
     * checks of a user with this login already exists; if yes, an exception is thrown. The
     * method does not persist the user; this needs to be done by the caller.
     *
     * @param login login of the user to create
     * @param firstName first name of the user to create
     * @param lastName last name of the user to create
     * @return the newly created user.
     */
    public KiWiUser createUser(final String login, final String firstName, final String lastName, final String password) throws UserExistsException;



    /**
      * Return the anonymous user. If it does not exist yet, it is created in the database and stored.
      *
      * @return
      */
     public KiWiUser getAnonymousUser();


    /**
     * Return a user by login. The user is looked up in the database and returned. In case
     * no user with this login is found, this method returns null.
     *
     * @param login the login to look for
     * @return the user with the given login, or null if no such user exists
     * @see kiwi.core.api.user.UserService#getUserByLogin(java.lang.String)
     */
    public KiWiUser getUserByLogin(String login);



    /**
      * Return a user by login. The user is looked up in the database and returned. In case
      * no user with this login is found, this method returns null.
      *
      * @param resource the resource to look for
      * @return the user with the given resource, or null if no such user exists
      * @see kiwi.core.api.user.UserService#getUserByLogin(java.lang.String)
      */
    public KiWiUser getUserByResource(KiWiResource resource);

    /**
     * Check whether the user with the given login name already exists.
     *
     * @param login
     * @return
     */
    public boolean userExists(String login);


}
